#!/bin/sh
# This script will create cgroups for DOMjudge to use in /sys/fs/cgroup/
# They need to be re-created after system boot; you may want to
# add this script to an init script.

# Change the following to the user you start the judgedaemon as.
# (hence: not the 'domjudge-run' user!)

JUDGEHOSTUSER=@DOMJUDGE_USER@
CGROUPBASE=@judgehost_cgroupdir@

for i in cpuset memory; do
    mkdir -p $CGROUPBASE/$i
    if [ ! -d $CGROUPBASE/$i/ ]; then
        if ! mount -t cgroup -o$i $i $CGROUPBASE/$i/; then
            echo "Error: Can not mount $i cgroup. Probably cgroup support is missing from running kernel. Unable to continue.

            To fix this, please make the following changes:
            1. In /etc/default/grub, add 'cgroup_enable=memory swapaccount=1' to GRUB_CMDLINE_LINUX_DEFAULT
            2. Run update-grub
            3. Reboot" >&2
            exit 1
        fi
    fi
    mkdir -p $CGROUPBASE/$i/domjudge
done

if [ ! -f $CGROUPBASE/memory/memory.limit_in_bytes ] || [ ! -f $CGROUPBASE/memory/memory.memsw.limit_in_bytes ]; then
    echo "Error: cgroup support missing memory features in running kernel. Unable to continue.

    To fix this, please make the following changes:
    1. In /etc/default/grub, add 'cgroup_enable=memory swapaccount=1' to GRUB_CMDLINE_LINUX_DEFAULT
    2. Run update-grub
    3. Reboot" >&2
    exit 1
fi

chown -R $JUDGEHOSTUSER $CGROUPBASE/*/domjudge

cat $CGROUPBASE/cpuset/cpuset.cpus > $CGROUPBASE/cpuset/domjudge/cpuset.cpus
cat $CGROUPBASE/cpuset/cpuset.mems > $CGROUPBASE/cpuset/domjudge/cpuset.mems
